<template>
	<view class="idiomSearch_container">
		<!-- 输入框 -->
		<view class="search_container">
			<u-input v-model="searchVal" border="true" placeholder="请输入成语" style="width: 100%;" />
			<view class="search_btn bg-gradual-green" @click="getData()">
				查询
			</view>
		</view>
		<u-toast ref="uToast" />
		<view class="load_container" v-if="loading">
			<u-loading mode="circle" :show="loading" color="#39b54a" size="60"></u-loading>
		</view>
		<view class="search_content light bg-grey" v-if="idiom_info.word">
			<view class="title">
				{{idiom_info.word}}
			</view> 
			<view class="pinyin">
				拼音：{{idiom_info.pinyin}}
			</view>
			<view class="item_container">
				<view class="label">解释：</view>
				<view class="content">
					{{idiom_info.explanation}}
				</view>
			</view>
			<view class="item_container">
				<view class="label">举例：</view>
				<view class="content">
					{{idiom_info.example}}
				</view>
			</view>
			<view class="item_container">
				<view class="label">出自：</view>
				<view class="content">
					{{idiom_info.derivation}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getIdiom
	} from "../api/remoteApi.js"
	export default {
		data() {
			return {
				searchVal: "洋洋得意",
				idiom_info: {
					word: "",
					pinyin: "",
					explanation: "",
					example: "",
					derivation: ""
				},
				loading:false
			}
		},
		methods: {
			async getData() {
				if (this.searchVal == "") {
					this.$refs.uToast.show({
						title: '请输入成语',
						type: 'error',
						icon: false,
					})
					return
				}
				this.loading = true
				const res = await getIdiom({
					searchVal: this.searchVal
				})
				this.loading = false
				if (res.success) {
					console.log("成语", res);
					this.idiom_info = res.idiom_info
				}
				if (res.error) {
					this.$refs.uToast.show({
						title: res.error,
						type: 'error',
						icon: false,
						position: "top"
					})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.idiomSearch_container {
		width: 100%;
		height: 100vh;
		margin: 0 auto;
		padding: 280rpx 40rpx 0 40rpx;
		box-sizing: border-box;
		background-color: #fff;
		position: relative;

		.search_container {
			width: 90%;
			position: absolute;
			left: 50%;
			top: 160rpx;
			transform: translate(-50%, -50%);
			display: flex;
			flex-direction: column;
			align-items: center;

			/deep/ .data-v-3fcd3222 view {
				border-color: #39b54a !important;
				border-radius: 20rpx;
			}

			.search_btn {
				margin-top: 20rpx;
				width: 200rpx;
				height: 70rpx;
				border-radius: 16rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.load_container{
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			display: flex;
			justify-content: center;
		}

		.search_content {
			width: 100%;
			height: 70vh;
			border-radius: 10rpx;
			padding: 20rpx;
			box-sizing: border-box;
			color: #010;

			.title {
				font-weight: bold;
				font-size: 36rpx;
				margin: 20rpx 0;
				color: #39b54a;
			}

			.pinyin {
				color: #1cbbb4;
				margin: 20rpx 0;
			}

			.item_container {
				display: flex;
				margin: 20rpx 0;

				.label {
					width: 90rpx;
				}

				.content {
          flex: 1;
        }
			}
		}
	}
</style>